//
//  TravelMapViewController.m
//  EDPS
//
//  Created by 张维扬 on 15/12/2.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "TravelMapViewController.h"
#import <AMapNaviKit/AMapNaviKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
@interface TravelMapViewController ()<MAMapViewDelegate, AMapSearchDelegate>
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) NSMutableArray *annotations;
@end

@implementation TravelMapViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
- (id)init
{
    self = [super init];
    if (self)
    {
        [self initAnnotations];
    }
    return self;
}
- (void)initAnnotations
{
    self.annotations = [NSMutableArray array];
    NSLog(@"%@", self.model.latitude);
    CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(self.model.latitude.integerValue, self.model.longitude.integerValue);
//    CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(33.346346, 100.29857239);
    MAPointAnnotation *a1 = [[MAPointAnnotation alloc] init];
    a1.coordinate = coordinates;
    [self.annotations addObject:a1];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initMapView];
}
#pragma mark - 初始化一个地图
- (void)initMapView
{
    // 高德key
    [MAMapServices sharedServices].apiKey = @"8379daa9b8e15973bcb311651e7cb272";
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    self.mapView.delegate = self;
    // 打开定位功能
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    [self.view addSubview:_mapView];
    // 添加大头针
    [self addAnnotation];
    
}
- (void)addAnnotation
{
    [self.mapView addAnnotations:self.annotations];
    [self.mapView showAnnotations:self.annotations animated:YES];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        // 重用池
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
        }
        // 大头针阴影
        annotationView.canShowCallout = YES;
        // 掉落动画
        annotationView.animatesDrop = YES;
        // 是否支持拖动
        annotationView.draggable  = YES;
        // 显示在气泡 左/右 侧的view
//        annotationView.leftCalloutAccessoryView = 
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        // 大头针的颜色
        annotationView.pinColor = MAPinAnnotationColorRed;
        // 返回大头针视图
        return annotationView;
    }
    
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
